// ------------------------------------------------------------------------------------------------------------------------
// Plemiona.pl 5.X Skrypt użytkowy
//
// Nazwa:      Podświetlanie spichlerzy w zależności od liczby surowców
// Wersja:     1.2.0003
// Autor:      Nexces (Forum Plemion)
// Dostosował: Lukasz032 (Plemiona Ś7)
//
// Tagi specjalne:
// Licencja:   Creative Commons Uznanie autorstwa - Brak komercyjnego zastosowania - Na tych samych warunkach 2.0 Polska
// Informacje: http://creativecommons.org/licenses/by-nc-sa/3.0/deed.pl
// Support:    lvwnbrz@lykamspam.pl
//
// ------------------------------------------------------------------------------------------------------------------------

// ==UserScript==
// @name           Podświetlanie spichlerzy w zależności od liczby surowców
// @namespace      http://code.google.com/p/plemiona-skrypty/
// @description    brak opisu
// @version        1.2.0003
// @include        http://pl*.plemiona.pl/game.php?*screen=overview_villages*
// @license        Creative Commons 3.0 BY-NC-SA (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.pl)
// @author         Nexces
// @contributor    Lukasz023 (Plemiona Ś7)
// ==/UserScript==

function PL_highlight_resources() {
	var p_up = 90;
	var p_low = 60;
	var color_range = new Array(100,230);


	var allTables = null;
	allTables = document.getElementsByTagName('table');


	var villageTable = null;
	for (var i=0; i < allTables.length; i++) {
		if (allTables[i].getAttribute('class') == 'vis') {
			if (allTables[i].innerHTML.indexOf('Surowce') >= 0) {
				villageTable = allTables[i];
				break;
			}
		}
	}

	if(villageTable == null) {
		return;
	}

	var allRows = villageTable.getElementsByTagName('tr');
	re_wood = /((<IMG[^>]+holz[^>]+>)(\d+)(<SPAN class="grey">.<\/SPAN>)?(\d+))/i;
	re_brick = /((<IMG[^>]+lehm[^>]+>)(\d+)(<SPAN class="grey">.<\/SPAN>)?(\d+))/i;
	re_iron = /((<IMG[^>]+eisen[^>]+>)(\d+)(<SPAN class="grey">.<\/SPAN>)?(\d+))/i;
	re_capacity = /^\d+$/;

	for (var i=1; i<allRows.length; i++) {
		var allCells = allRows[i].getElementsByTagName('td');
		// capacity scan
		for (var j=0; j <allCells.length; j++) {
			var capacity_test = re_capacity.exec(allCells[j].innerHTML);
			if (capacity_test) {
				if (capacity_test>=1000) {
					capacity = capacity_test;
					break;
				}
			}
		}
		if ((typeof capacity) == "undefined") {
			return;
		}
		for (var j=0; j <allCells.length; j++) {
			if (allCells[j].innerHTML.indexOf('/graphic/holz.png') > 0) {
				result_wood = re_wood.exec(allCells[j].innerHTML);
				result_brick = re_brick.exec(allCells[j].innerHTML);
				result_iron = re_iron.exec(allCells[j].innerHTML);

				if(result_wood) {
					var wood = result_wood[3] + result_wood[5];
					var p_fillness = Math.round(wood*100/capacity);
					if (p_fillness > p_low) {
						var p_alert = (((p_fillness-p_low)*100)/(p_up-p_low));
						p_alert = p_alert > 100 ? 100 : p_alert;
						var color = Math.round((((color_range[1]-color_range[0])*p_alert)/100)+color_range[0]);
						allCells[j].innerHTML = allCells[j].innerHTML.replace(re_wood,'<span style="color: rgb('+color+',0,0);" title="Wypełniony w '+p_fillness+'%">$1</span>');
					} else {
						allCells[j].innerHTML = allCells[j].innerHTML.replace(re_wood,'<span title="Wypełniony w '+p_fillness+'%">$1</span>');
					}
				}
				if(result_brick) {
					var brick = result_brick[3] + result_brick[5];
					var p_fillness = Math.round(brick*100/capacity);
					if (p_fillness > p_low) {
						var p_alert = (((p_fillness-p_low)*100)/(p_up-p_low));
						p_alert = p_alert > 100 ? 100 : p_alert;
						var color = Math.round((((color_range[1]-color_range[0])*p_alert)/100)+color_range[0]);
						allCells[j].innerHTML = allCells[j].innerHTML.replace(re_brick,'<span style="color: rgb('+color+',0,0);" title="Wypełniony w '+p_fillness+'%">$1</span>');
					} else {
						allCells[j].innerHTML = allCells[j].innerHTML.replace(re_brick,'<span title="Wypełniony w '+p_fillness+'%">$1</span>');
					}
				}

				if(result_iron) {
					var iron = result_iron[3] + result_iron[5];
					var p_fillness = Math.round(iron*100/capacity);
					if (p_fillness > p_low) {
						var p_alert = (((p_fillness-p_low)*100)/(p_up-p_low));
						p_alert = p_alert > 100 ? 100 : p_alert;
						var color = Math.round((((color_range[1]-color_range[0])*p_alert)/100)+color_range[0]);
						allCells[j].innerHTML = allCells[j].innerHTML.replace(re_iron,'<span style="color: rgb('+color+',0,0);" title="Wypełniony w '+p_fillness+'%">$1</span>');
					} else {
						allCells[j].innerHTML = allCells[j].innerHTML.replace(re_iron,'<span title="Wypełniony w '+p_fillness+'%">$1</span>');
					}
				}


			}
		}
	}
}

PL_highlight_resources();